package com.guguskins.back.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guguskins.back.entity.GuCurrencyRecord;
import com.guguskins.back.entity.vo.PageVo;
import com.guguskins.back.mapper.GuCurrencyRecordMapper;
import com.guguskins.back.service.GuCurrencyRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mybatis-plus
 * @since 2021-07-21
 */
@Service
public class GuCurrencyRecordServiceImpl extends ServiceImpl<GuCurrencyRecordMapper, GuCurrencyRecord> implements GuCurrencyRecordService {

    @Autowired
    GuCurrencyRecordMapper recordMapper;

    @Override
    public PageVo<GuCurrencyRecord> getPage(Integer pageNum, Integer pageSize, String userName, Integer sourceType, String creatTime) {
        if (pageNum == 1) {
            pageNum = 0;
        } else {
            pageNum = (pageNum - 1) * pageSize;
        }
        if (creatTime != null) {
            creatTime = creatTime.substring(0, 10);
        }


        List<GuCurrencyRecord> records = recordMapper.findByPage(pageNum, pageSize, userName, sourceType, creatTime);
        Long count = recordMapper.findBycount();
        PageVo<GuCurrencyRecord> pageVo = new PageVo<>();
        pageVo.setTotal(count);
        pageVo.setData(records);
        return pageVo;
    }

    @Override
    public List<GuCurrencyRecord> getLike(String userName, Integer sourceType, String creatTime) {

        return recordMapper.findLike(userName, sourceType, creatTime);
    }

    @Override
    public boolean addRecord(Long userId, String steamId, String nickname, BigDecimal amount, BigDecimal originalAmount, int type, int sourceType, Long sourceId, String sourceImage, String sourceName, String desc, String remark) {
        GuCurrencyRecord currencyRecord = new GuCurrencyRecord();
        currencyRecord.setCrUserId(userId);
        currencyRecord.setCrSteamId(steamId);
        currencyRecord.setCrUserNickname(nickname);
        currencyRecord.setCrAmount(amount);
        currencyRecord.setCrOriginalAmount(originalAmount);
        currencyRecord.setCrType(type);
        currencyRecord.setCrSourceType(sourceType);
        currencyRecord.setCrSourceId(sourceId);
        currencyRecord.setCrSourceImage(sourceImage);
        currencyRecord.setCrSourceName(sourceName);
        currencyRecord.setCrDesc(desc);
        currencyRecord.setCrRemark(remark);
        return currencyRecord.insert();
    }
}
